package JPetStore.web.servlet;

import JPetStore.domain.Account;
import JPetStore.domain.Order;
import JPetStore.service.LogService;
import JPetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ViewListOrderServlet extends HttpServlet {
    private static final String VIEWLISTORDER = "/WEB-INF/jsp/order/ListOrders.jsp";
    private String username;
    private OrderService orderService;
    private List<Order> orderList = new ArrayList();

    public ViewListOrderServlet() {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account account = (Account)session.getAttribute("loginAccount");
        this.username = account.getUsername();
        this.orderService = new OrderService();
        this.orderList = this.orderService.getOrdersByUsername(this.username);
        session.setAttribute("orderList", this.orderList);
        if (account != null) {
            String username = account.getUsername();
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + req.getContextPath() + req.getServletPath() + "?" + (req.getQueryString());
            LogService logService = new LogService();
            String logInfo = logService.logInfo(new Object[]{" "}) + strBackUrl + "查看了订单";
            logService.insertLogInfo(username, logInfo);
        }
        req.getRequestDispatcher(VIEWLISTORDER).forward(req, resp);
    }
}
